﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

namespace GameCore
{
    /// <summary>
    /// ui图集类
    /// </summary>
	[CreateAssetMenu(menuName = "自定义/图集")]
	public class UIAtlas : ScriptableObject
    {
        /// <summary>
        /// 图集中的精灵
        /// </summary>
        [SerializeField]
        private List<Sprite> spriteList;

        private Dictionary<string, Sprite> spriteMap;

        private void AnalyticData()
        {
            if (spriteMap != null)
                return;
            spriteMap = new Dictionary<string, Sprite>();
            for (int i = 0; i < spriteList.Count; ++i)
            {
                spriteMap.Add(spriteList[i].name, spriteList[i]);
            }
        }

        public void InitAtlas(List<Sprite> spriteList)
        {
            this.spriteList = spriteList;
        }

        public List<string> GetAllSpriteName()
        {
            var spriteNames = new List<string>();
            for (int i = 0; i < spriteList.Count; ++i)
            {
                spriteNames.Add(spriteList[i].name);
            }
            return spriteNames;
        }

        /// <summary>
        /// 获取sprite
        /// </summary>
        public Sprite GetSprite(string name)
        {
            AnalyticData();
            Sprite sprite = null;
            spriteMap.TryGetValue(name, out sprite);
            return sprite;
        }

		/// <summary>
		/// 获取sprites
		/// </summary>
		public Sprite[] GetSprites(string name)
		{
			List<Sprite> list = new List<Sprite> ();
			foreach (var each in spriteMap) {
				if (each.Key.Contains (name))
					list.Add (each.Value);
			}
			return list.ToArray ();
		}
    }

}

